"""
start north rotate clockwise
0 = out, circle
1 = out, square
2 = in, circle
3 = in, square
0, 2 go together
1, 3 go together

puzzle:

0   1

2   3

"""
import random


#VARIABLE DECLARATION
pieces = [
    [1,0,1,1],
    [0,2,3,2],
    [1,1,1,0],
    [3,1,2,0],
    [1,3,0,3],
    [3,2,2,1],
    #[0,1,0,1],
    #[2,2,2,3],
    #[0,2,0,0]
    ]


connections = []
solved = False

#FUNCTION DECLARATION
def rotate(x):
    #rotate
    for i in range(0,3):
        z = random.randint(0,3)
        count = 0
        while count < z:
            top = x[i][0]
            x[i].pop(0)
            x[i].append(top)
            count += 1
    return x

def flip(x):
    for i in range(0,3):
        z = random.randint(0,1)
        if z == 1:
            top = x[i][0]
            x[i][0] = x[i][2]
            x[i][2] = top
    return x

# this function shuffles the puzzle randomly
def rand_s(p):
    random.shuffle(p)
    p = rotate(p)
    p = flip(p)
    return p

# next there should be a function which checks to see if the value is valid
def is_solved(x):
    if not are_connected(x[0][1],x[1][3]):
        return False
    if not are_connected(x[1][1],x[2][3]):
        return False
    if not are_connected(x[0][2],x[3][0]):
        return False
    if not are_connected(x[1][2],x[4][0]):
        return False
    if not are_connected(x[2][2],x[5][0]):
        return False
    if not are_connected(x[3][1],x[4][3]):
        return False
    if not are_connected(x[4][1],x[5][3]):
        return False
    '''if not are_connected(x[3][2],x[6][0]):
        return False
    if not are_connected(x[4][2],x[7][0]):
        return False
    if not are_connected(x[5][2],x[8][0]):
        return False
    if not are_connected(x[6][1],x[7][3]):
        return False
    if not are_connected(x[7][1],x[8][3]):
        return False'''
    else:
        return True

def are_connected(x,y):
    if (x == 0 and y == 2 or x == 1 and y == 3 or x == 2 and y == 0 or x == 3 and y == 1):
        return True
    return False

    
#MAIN LOOP
while not(solved):
    #pieces = rand_s(pieces)
    #print("not solved.")
    if is_solved(pieces):
        solved = True
        print("solved.")
        print(str(pieces))
